package com.kexio.common.dto;

import java.util.Collections;
import java.util.List;

/**
 * 分页响应结果
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public class PageResponse<T> {
    
    /**
     * 当前页数据
     */
    private List<T> records;
    
    /**
     * 当前页码
     */
    private Integer page;
    
    /**
     * 每页大小
     */
    private Integer size;
    
    /**
     * 总记录数
     */
    private Long total;
    
    /**
     * 总页数
     */
    private Integer pages;
    
    /**
     * 是否有上一页
     */
    private Boolean hasPrevious;
    
    /**
     * 是否有下一页
     */
    private Boolean hasNext;
    
    public PageResponse() {
        this.records = Collections.emptyList();
    }
    
    public PageResponse(List<T> records, Integer page, Integer size, Long total) {
        this.records = records == null ? Collections.emptyList() : records;
        this.page = page;
        this.size = size;
        this.total = total;
        this.pages = calculateTotalPages(total, size);
        this.hasPrevious = page > 1;
        this.hasNext = page < this.pages;
    }
    
    /**
     * 计算总页数
     */
    private Integer calculateTotalPages(Long total, Integer size) {
        if (total == null || total <= 0 || size == null || size <= 0) {
            return 0;
        }
        return (int) Math.ceil((double) total / size);
    }
    
    /**
     * 创建分页响应
     */
    public static <T> PageResponse<T> of(List<T> records, Integer page, Integer size, Long total) {
        return new PageResponse<>(records, page, size, total);
    }
    
    /**
     * 创建空的分页响应
     */
    public static <T> PageResponse<T> empty(Integer page, Integer size) {
        return new PageResponse<>(Collections.emptyList(), page, size, 0L);
    }
    
    /**
     * 从PageRequest创建空响应
     */
    public static <T> PageResponse<T> empty(PageRequest pageRequest) {
        return empty(pageRequest.getPage(), pageRequest.getSize());
    }
    
    /**
     * 判断是否为空
     */
    public boolean isEmpty() {
        return records == null || records.isEmpty();
    }
    
    /**
     * 获取记录数量
     */
    public int getRecordCount() {
        return records == null ? 0 : records.size();
    }
    
    // Getters and Setters
    public List<T> getRecords() {
        return records;
    }
    
    public void setRecords(List<T> records) {
        this.records = records == null ? Collections.emptyList() : records;
    }
    
    public Integer getPage() {
        return page;
    }
    
    public void setPage(Integer page) {
        this.page = page;
    }
    
    public Integer getSize() {
        return size;
    }
    
    public void setSize(Integer size) {
        this.size = size;
    }
    
    public Long getTotal() {
        return total;
    }
    
    public void setTotal(Long total) {
        this.total = total;
        this.pages = calculateTotalPages(total, size);
    }
    
    public Integer getPages() {
        return pages;
    }
    
    public void setPages(Integer pages) {
        this.pages = pages;
    }
    
    public Boolean getHasPrevious() {
        return hasPrevious;
    }
    
    public void setHasPrevious(Boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }
    
    public Boolean getHasNext() {
        return hasNext;
    }
    
    public void setHasNext(Boolean hasNext) {
        this.hasNext = hasNext;
    }
    
    @Override
    public String toString() {
        return "PageResponse{" +
                "records=" + (records == null ? 0 : records.size()) + " items" +
                ", page=" + page +
                ", size=" + size +
                ", total=" + total +
                ", pages=" + pages +
                ", hasPrevious=" + hasPrevious +
                ", hasNext=" + hasNext +
                '}';
    }
}
